package com.star.order.controller;


import com.star.entity.Result;
import com.star.entity.StatusCode;
import com.star.order.config.TokenDecode;
import com.star.order.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/*
			  ________   ___   ___        ___   ________   ___   ___        ___
	 ________|\   __  \_|\  \_|\  \______|\  \_|\   __  \_|\  \_|\  \______|\  \_________
	 \       \ \  \_\ /_\ \  \\ \  \     \ \  \\ \  \_\ /_\ \  \\ \  \     \ \  \        \
	  \       \ \   __  \\ \  \\ \  \     \ \  \\ \   __  \\ \  \\ \  \     \ \  \        \
	   \       \ \  \_\  \\ \  \\ \  \____ \ \  \\ \  \_\  \\ \  \\ \  \____ \ \  \        \
	    \       \ \_______\\ \__\\ \_______\\ \__\\ \_______\\ \__\\ \_______\\ \__\        \
	     \       \|_______| \|__| \|_______| \|__| \|_______| \|__| \|_______| \|__|         \
	 	  \___________________________________________________________________________________\

*/
@RestController
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private TokenDecode tokenDecode;
    @Autowired
    private CartService cartService;

    /**
     * 添加购物车
     * @param skuId
     * @param num 商品数量
     * @return
     */
    @GetMapping("/addCart")
    public Result addCart(@RequestParam("skuId") String skuId,@RequestParam("num") Integer num) {
//        String username = "itcast";
        String username = tokenDecode.getUserInfo().get("username");
        cartService.addCart(skuId, num,username);
        return new Result(true, StatusCode.OK, "购物车添加成功!");
    }

    /**
     * 查询购物车列表
     * @return
     */
    @GetMapping("/list")
    public Map list() {
//        String username = "itcast";
        String username = tokenDecode.getUserInfo().get("username");
        Map map = cartService.list(username);
        return map;
    }
}
